package com.htime.controller;

import com.htime.domain.CommonRes;
import com.htime.domain.User;
import com.htime.domain.UserRes;
import com.htime.intercepter.ValidationService;
import com.htime.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by HTime on 2017/3/5.
 */
@RestController
public class UserRestController {

    @Autowired
    UserService userService;

    @Autowired
    ValidationService validationService;

    @RequestMapping(value = "/auth", method = RequestMethod.GET)
    public UserRes auth(
            @RequestParam(  value = "userName",required = true ) String userName ,
            @RequestParam(  value = "userPwd",required = true ) String userPwd) {

        UserRes res = new UserRes();
        User user = userService.getUserByAccount(userName, userPwd);
        if(null == user) {
             res.setError(CommonRes.RES_NOT_ALLOW, "账号密码错误");
        } else {
            res.setUser(user);
            //生成token
            String token = validationService.createToken(user);
            res.setToken(token);
        }
        return res;
    }

}
